package org.icress.fusion.admin

class PageLayoutController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 100, 100)
        render view : '/admin/pageLayout/list', model : [pageLayoutInstanceList: PageLayout.list(params), pageLayoutInstanceTotal: PageLayout.count()]
    }

    def create = {
        def pageLayoutInstance = new PageLayout()
        pageLayoutInstance.properties = params
        render view : '/admin/pageLayout/create', model : [pageLayoutInstance: pageLayoutInstance]
    }

    def save = {
        def pageLayoutInstance = new PageLayout(params)
        pageLayoutInstance.validate()
        if (!pageLayoutInstance.hasErrors() && pageLayoutInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'admin.pageLayout.label', default: 'PageLayout'), pageLayoutInstance.id])}"
            redirect(action: "show", id: pageLayoutInstance.id)
        }else{
            render(view: "/admin/pageLayout/create", model: [pageLayoutInstance: pageLayoutInstance])
        }
    }

    def show = {
        def pageLayoutInstance = PageLayout.get(params.id)
        if (!pageLayoutInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.pageLayout.label', default: 'PageLayout'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/admin/pageLayout/show', model : [pageLayoutInstance: pageLayoutInstance]
        }
    }

    def edit = {
        def pageLayoutInstance = PageLayout.get(params.id)
        if (!pageLayoutInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.pageLayout.label', default: 'PageLayout'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/admin/pageLayout/edit', model : [pageLayoutInstance: pageLayoutInstance]
        }
    }
    
    def update = {
        def pageLayoutInstance = PageLayout.get(params.id)
        if (pageLayoutInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (pageLayoutInstance.version > version) {
                    
                    pageLayoutInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'admin.pageLayout.label', default: 'PageLayout')] as Object[], "Another user has updated this PageLayout while you were editing")
                    render(view: "/admin/pageLayout/edit", model: [pageLayoutInstance: pageLayoutInstance])
                    return
                }
            }
            pageLayoutInstance.properties = params
            pageLayoutInstance.validate()
            if (!pageLayoutInstance.hasErrors() && pageLayoutInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'admin.pageLayout.label', default: 'PageLayout'), pageLayoutInstance.id])}"
                redirect(action: "show", id: pageLayoutInstance.id)
            }else {
                render(view: "/admin/pageLayout/edit", model: [pageLayoutInstance: pageLayoutInstance])
            }
        } else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.pageLayout.label', default: 'PageLayout'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def pageLayoutInstance = PageLayout.get(params.id)
        if (pageLayoutInstance) {
            try {
                pageLayoutInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'admin.pageLayout.label', default: 'PageLayout'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'admin.pageLayout.label', default: 'PageLayout'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.pageLayout.label', default: 'PageLayout'), params.id])}"
            redirect(action: "list")
        }
    }
}
